#include "CreateLineWidget.h"

#include "Core/MainWindow.h"
#include <vtkProperty.h>
#include <QMessageBox>
#include "FeatureShape/FeatureShape.h"

CreateLineWidget::CreateLineWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateCurveWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateLineWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreateLineWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateLineWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateLineWidget::onApply);

	ui->startWidget->installEventFilter(this);
	ui->endWidget->installEventFilter(this);

	connect(ui->p1X, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);
	connect(ui->p1Y, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);
	connect(ui->p1Z, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);

	connect(ui->p2X, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);
	connect(ui->p2Y, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);
	connect(ui->p2Z, &QDoubleSpinBox::valueChanged, this, &CreateLineWidget::onPosChanged);

	ui->startWidget->setStyleSheet("background-color:rgb(255,178,102)");

	m_startPnt = new RWTripleFloatInput(ui->p1X, ui->p1Y, ui->p1Z);
	m_endPnt = new RWTripleFloatInput(ui->p2X, ui->p2Y, ui->p2Z);

	m_mainWindow->SetSelectionMode(SM_Vertex);

	// operation -> param
	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeLine* LineShape = static_cast<FeatureShapeLine*>(CreateOP->m_featureShape.get());
		m_startPnt->SetValue(LineShape->_start);
		m_endPnt->SetValue(LineShape->_end);
	}

	cacheActor = m_mainWindow->createLine(gp_Pnt(m_startPnt->GetValue()), gp_Pnt(m_endPnt->GetValue()));
}

CreateLineWidget::~CreateLineWidget()
{
	delete ui;
	delete m_startPnt;
	delete m_endPnt;
}

void CreateLineWidget::onPosChanged()
{
	if (!cacheActor)
	{
		return;
	}
	// 批量修改
	if (bBatchChange)
	{
		return;
	}

	UpdateInner();
}

void CreateLineWidget::OnPickPosition(const gp_Pnt& inPosition)
{
	bBatchChange = true;
	if (focusIndex == 0)
	{
		m_startPnt->SetValue(inPosition);
	}
	else if (focusIndex == 1)
	{
		m_endPnt->SetValue(inPosition);
	}
	bBatchChange = false;

	UpdateInner();
}

ShapeOperation* CreateLineWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("直线"), cacheActor->m_featureShape);
	return op;
}

void CreateLineWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeLine(cacheActor->OccShape(), m_startPnt->GetValue(), m_endPnt->GetValue()));
}

bool CreateLineWidget::eventFilter(QObject* obj, QEvent* event)
{
	QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
	if (event->type() == QEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton)
	{
		if (obj == ui->startWidget && focusIndex != 0)
		{
			ui->startWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->endWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = 0;
		}
		if (obj == ui->endWidget && focusIndex != 1)
		{
			ui->endWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->startWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = 1;
		}
	}

	return QObject::eventFilter(obj, event);
}

void CreateLineWidget::Reset()
{
	QRWCreateCurveWidget::Reset();

	m_startPnt->SetValue(gp_Pnt());
	m_endPnt->SetValue(gp_Pnt(0, 0, 1));

	cacheActor = m_mainWindow->createLine(gp_Pnt(m_startPnt->GetValue()), gp_Pnt(m_endPnt->GetValue()));
	onPosChanged();
}

void CreateLineWidget::UpdateInner()
{
	gp_Pnt p1(ui->p1X->value(), ui->p1Y->value(), ui->p1Z->value());
	gp_Pnt p2(ui->p2X->value(), ui->p2Y->value(), ui->p2Z->value());

	if (p1.IsEqual(p2, Precision::Confusion()))
	{
        QMessageBox::warning(this, "Error", "Points are the same");
        return;
	}
	
	cacheActor = m_mainWindow->UpdateLineActor(cacheActor, p1, p2);
}
